package com.jy.wms.service;

import com.jy.wms.common.annotions.BaseServiceDescribe;
import com.jy.wms.common.base.BaseService;
import com.jy.wms.common.base.ServiceException;
import com.jy.wms.common.base.SucessException;
import com.jy.wms.pojo.WmsCustomerFile;
import com.jy.wms.dao.WmsCustomerFileMapper;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import java.util.List;
import java.util.Map;

/**
    * @ClassName: WmsCustomerFileService.java
    * @Description: TODO(这里用一句话描述这个类的作用)
    * @Author Liyi
    * @Date Wed Oct 11 11:26:05 CST 2017
    */

    @Service
    public class WmsCustomerFileService extends BaseService<WmsCustomerFileMapper,WmsCustomerFile> {

    /**
     * 获取货主下拉列表选项
     * @return Map<String,Object>
     */
    @BaseServiceDescribe(moduleName = "客户档案管理",btnName = "获取货主下拉列表选项")
     public List<Map<String,Object>> getOwner(Map<Object,String> map)throws Exception{
         try {
             return dao.getOwner( map );
         } catch (Exception e) {
             throw new ServiceException("获取货主下拉列表选项",e);
         }
     }

     /**
     * 插入一条记录
     * @param entity 实体对象
     * @return int
     */
     @BaseServiceDescribe(moduleName = "客户档案管理",btnName = "新增客户档案基本信息")
    public void baseInsert( WmsCustomerFile entity)throws SucessException,ServiceException{
         Integer KhmcCount = dao.recheckingByKhmc(entity.getKhmc(), entity.getSshzid());
         Integer KhbhCount = dao.recheckingByKhbh(entity.getKhbh(), entity.getSshzid());
        if(KhmcCount > 0){
            throw new ServiceException("客户名称重复");
        }else if(KhbhCount > 0){
            throw new ServiceException("客户编号重复");
        }else{
            Integer integer = dao.insert(entity);
            if(integer > 0){
                throw new SucessException("新增客户档案基本信息成功");
            }else{
                throw new ServiceException("新增客户档案基本信息失败，未插入数据库");
            }
        }
    }

   /**
    * 插入一条记录（匹配有值的字段）
    * @param entity 实体对象
    * @return int
    */
   @BaseServiceDescribe(moduleName = "客户档案管理",btnName = "新增客户档案基本信息")
    public Integer baseInsertSelective( WmsCustomerFile entity)throws SucessException,ServiceException{
       Integer addCunt =  dao.baseInsertSelective( entity);
       if(addCunt>0){
           throw new SucessException("新增客户档案基本信息成功");
       }else{
           throw new ServiceException("新增客户档案基本信息失败，未插入数据库");
       }
   }

   /**
    * 删除（根据主键ID删除）
    * @param id 主键
    * @return int
    */
    public Integer baseDeleteByPrimaryKey( String id){
         return dao.baseDeleteByPrimaryKey( id);
    }

   /**
    * 删除（根据ID 批量删除）
    * @param idList 主键ID列表
    * @return int
    */
    public Integer baseDeleteBathPrimaryKeys( List<String> idList){
         return dao.baseDeleteBathPrimaryKeys( idList );
    }

    /**
     * 路线下拉列表选择
     * @param xlmc 路线名称
     * @return
     */

    @BaseServiceDescribe(moduleName = "客户档案管理",btnName = "查询路线")
    public List<Map<String,Object>> baseSelectLine(String xlmc,String sshzid)throws Exception{
        try {
            return dao.baseSelectLine(xlmc,sshzid);
        } catch (Exception e) {
            throw new ServiceException("查询路线",e);
        }
    }
    /**
    * 分页查询
    * @param entity 实体
    * @return Page<WmsCustomerFile>
    */
    @BaseServiceDescribe(moduleName = "客户档案管理",btnName = "分页查询客户档案信息")
    public Page<WmsCustomerFile> baseSelectPage(WmsCustomerFile entity )throws Exception{
        try {
            PageHelper.startPage(entity.getPage(),entity.getRows());
            return (Page<WmsCustomerFile>)dao.baseSelectPage( entity );
        } catch (Exception e) {
            throw new ServiceException("分页查询客户档案信息",e);
        }
    }

   /**
    * 查询（根据主键ID查询）
    * @param id 主键ID
    * @return WmsCustomerFile
    */
   @BaseServiceDescribe(moduleName = "客户档案管理",btnName = "查询唯一客户档案基本信息")
    public WmsCustomerFile baseSelectByPrimaryKey( String id )throws Exception{
        try {
            return dao.baseSelectByPrimaryKey( id );
        } catch (Exception e) {
             throw new ServiceException("查询唯一客户档案基本信息",e);
        }
    }

    /**
     * 判断当前客户名称是否已存在
     * @param khmc 客户名称
     * @return boolean
     */
    @BaseServiceDescribe(moduleName = "客户档案管理",btnName = "判断当前客户名称是否已存在")
    public boolean recheckingByKhmc(String khmc,String hzid)throws Exception{
        try {
            Integer count = dao.recheckingByKhmc(khmc,hzid);
            if(count>0) {
                return false;
            }else{
                return true;
            }
        } catch (Exception e) {
            throw new ServiceException("判断当前客户名称是否已存在",e);
        }
    }

   /**
    * 查询（根据ID 批量查询）
    * @param idList 主键ID列表
    * @return list
    */
    public Integer baseSelectBathPrimaryKeys( List<WmsCustomerFile> idList ){
         return dao.baseSelectBathPrimaryKeys( idList );
    }

   /**
    *修改 （匹配有值的字段）
    * @param entity 主键ID列表
    * @return int
    */
   @BaseServiceDescribe(moduleName = "客户档案管理",btnName = "修改客户档案基本信息")
    public Integer baseUpdateByPrimaryKeySelective( WmsCustomerFile entity)throws ServiceException,SucessException{
        Integer count = dao.recheckingByKhmc(entity.getKhmc(), entity.getSshzid());
        WmsCustomerFile wmsCustomerFile = dao.baseSelectByPrimaryKey(entity.getId());
        Boolean type = true;
        if(wmsCustomerFile.getKhmc().equals(entity.getKhmc()) && count > 1){
            type = false;
        }else if(!(wmsCustomerFile.getKhmc().equals(entity.getKhmc())) && count > 0){
            type = false;
        }
        if(type){
            Integer updateCount = dao.baseUpdateByPrimaryKeySelective( entity);
            if(updateCount>0){
                throw new SucessException("修改客户档案基本信息成功");
            }else{
                throw new ServiceException("修改客户档案基本信息失败，未插入数据库");
            }
        }else{
            throw new ServiceException("客户名称重复");
        }
    }

    /**
     *批量作废用户档案
     * @param ids 修改IDs
     * @return int
     */
    @BaseServiceDescribe(moduleName = "客户档案管理",btnName = "批量作废用户档案")
    public Integer cancelAll( List<String> ids)throws ServiceException,SucessException{
        Integer count = dao.cancelAll(ids);
        if(count>0){
            throw new SucessException("作废用户档案成功");
        }else{
            throw new ServiceException("作废用户档案失败，未插入数据库");
        }
    }

   /**
    *修改（根据主键ID修改）
    * @param entity 主键ID列表
    * @return int
    */
    public Integer baseUpdateByPrimaryKey( WmsCustomerFile entity){
         return dao.baseUpdateByPrimaryKey( entity);
    }
}
